import { fileURLToPath, URL } from 'node:url'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { lazyImport, VxeResolver } from 'vite-plugin-lazy-import'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import UnoCSS from 'unocss/vite'
import postcssPxToViewport from 'postcss-px-to-viewport'

// import vueDevTools from 'vite-plugin-vue-devtools'

// https://vite.dev/config/
export default ({ mode }) => {
  const env = loadEnv(mode, process.cwd())
  // console.log(env, 'env----')
  return defineConfig({
    base: '/dp/web',
    plugins: [
      vue(),
      // vueDevTools(),
      lazyImport({
        resolvers: [
          VxeResolver({
            libraryName: 'vxe-table',
          }),
          VxeResolver({
            libraryName: 'vxe-pc-ui',
          }),
        ],
      }),
      UnoCSS(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
    ],
    css: {
      postcss: {
        plugins: [
          postcssPxToViewport({
            unitToConvert: 'px', // 要转化的单位
            viewportWidth: 1920, // UI设计稿的大小
            unitPrecision: 6, // 转换后的精度
            viewportUnit: 'vw', // 转换后的单位
            fontViewportUnit: 'vw', // 字体转换后的单位
            propList: ['*'], // 能转换的属性，*表示所有属性
            selectorBlackList: ['ignore-'], // 指定不转换为视窗单位的类名
            minPixelValue: 2, // 最小转换的值
            mediaQuery: false, // 是否在媒体查询的css代码中也进行转换
            replace: true, // 是否转换后直接更换属性值
            exclude: [], // 忽略某些文件夹下的文件或特定文件
            include: [], // 包含那些文件或者特定文件
            landscape: false, // 是否处理横屏情况
          }),
        ],
      },
    },
    server: {
      proxy: {
        '/api': {
          target: 'https://zhdj.cdc.zj.cn:7070/',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''),
        },
      },
      // https: false,
      host: '0.0.0.0',
      port: 5173,
    },
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
      },
    },
    build: {
      sourcemap: env.VITE_USER_NODE_ENV === 'devlopment',
    },
  })
}
